package hubbub

import static org.junit.Assert.*
import org.junit.*

class UserIntegrationTestTests {

    @Before
    void setUp() {
        // Setup logic here
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

    @Test
    void testFirstSaveEver() {
        def user = new User(userId: 'Joe', password: 'secret',
			homepage: 'http://nontster.wordpress.com')
		assertNotNull user.save()
		assertNotNull user.id
		
		def foundUser = User.get(user.id)
		assertEquals 'Joe', foundUser.userId
    }
	
	@Test
	void testSaveAndUpdate() {
		def user = new User(userId: 'Joe', password: 'secret',
			homepage: 'http://nontster.wordpress.com')
		assertNotNull user.save()
		
		def foundUser = User.get(user.id)
		foundUser.password = 'sesame'
		foundUser.save()
		
		def editedUser =  User.get(user.id)		
		assertEquals 'sesame', editedUser.password
	}
	
	@Test
	void testSaveThenDelete() {
		def user = new User(userId: 'Joe', password: 'secret',
			homepage: 'http://nontster.wordpress.com')
		assertNotNull user.save()
		
		def foundUser = User.get(user.id)
		foundUser.delete()
		
		assertFalse User.exists(foundUser.id)
	}
	
	@Test
	void testEvilSave() {
		def user = new User(userId: 'chuck_norris', password: 'tiny',
			homepage: 'not-a-url')
		
		assertFalse user.validate()
		assertTrue user.hasErrors()
		
		def errors = user.errors
		
		assertEquals "size.toosmall",
			errors.getFieldError("password").code
		assertEquals "tiny",
			errors.getFieldError("password").rejectedValue
			
/*		assertEquals "url.invalid",
			errors.getFieldError("homepage").code
		assertEquals "not-a-url",
			errors.getFieldError("homepage").rejectedValue*/
			
		assertNull errors.getFieldError("userId")
		
	}
}
